'use strict';
const db = uniCloud.database();
const dbCmd = db.command;
const $ = dbCmd.aggregate;
const {
	getCommonTime,
	getFullTime
} = require('api-time')
exports.main = async (event, context) => {
	let rechargeDatas = await db.collection('wmzmall-shopie').where({
		is_report: dbCmd.exists(false)
	}).limit(100).orderBy('create_time', 'desc').get()
	rechargeDatas = rechargeDatas.data
	let app_id
	let shopieReportData
	let money = 0
	let refound_money = 0
	let refound_count = 0
	let pay_count = 0
	let wechat_count = 0
	let alipay_count = 0
	let wechat_money = 0
	let alipay_money = 0
	let sm_count = 0
	let sm_money = 0
	let lj_count = 0
	let lj_money = 0
	let dd_count = 0
	let dd_money = 0
	let cz_count = 0
	let cz_money = 0
	let hy_count = 0
	let hy_money = 0
	let ye_count = 0
	let ye_money = 0

	let type
	let date_time
	let appletInfo
	for (var i in rechargeDatas) {
		money = 0
		pay_count = 0
		refound_money = 0
		refound_count = 0
		wechat_count = 0
		alipay_count = 0
		wechat_money = 0
		alipay_money = 0
		sm_count = 0
		sm_money = 0
		lj_count = 0
		lj_money = 0
		dd_count = 0
		dd_money = 0
		cz_count = 0
		cz_money = 0
		hy_count = 0
		hy_money = 0
		ye_count = 0
		ye_money = 0
		app_id = rechargeDatas[i].app_id
		type = rechargeDatas[i].type
		if (type === 1) {
			money = 0
			refound_count = 1
			refound_money = rechargeDatas[i].money
		} else {
			refound_money = 0
			pay_count = 1
			money = rechargeDatas[i].money
			if (rechargeDatas[i].way === '微信支付') {
				wechat_count = 1
				wechat_money = money
			} else if (rechargeDatas[i].way === '支付宝支付') {
				alipay_count = 1
				alipay_money = money
			} else if (rechargeDatas[i].way === '余额') {
				ye_count = 1
				ye_money = money
			}
			if (rechargeDatas[i].cate == '收款码订单') {
				sm_count = 1
				sm_money = money
			} else if (rechargeDatas[i].cate == '远程收单订单') {
				lj_count = 1
				lj_money = money
			} else if (rechargeDatas[i].cate == '商城订单') {
				dd_count = 1
				dd_money = money
			} else if (rechargeDatas[i].cate == '充值') {
				cz_count = 1
				cz_money = money
			} else if (rechargeDatas[i].cate == '会员') {
				hy_count = 1
				hy_money = money
			}
		}
		let create_time = rechargeDatas[i].create_time
		let cDate = getCommonTime(new Date(create_time));
		let cYear = cDate.now.year;
		let cMonth = cDate.now.month;
		let cDay = cDate.now.day;
		let cHours = cDate.now.hour;
		appletInfo = await db.collection('wmz-applet-info').doc(rechargeDatas[i].app_id).get()
		appletInfo = appletInfo.data[0]
		if (appletInfo.report_end_time && appletInfo.report_end_time != '23:59:59') {
			let tHours = parseInt(appletInfo.report_end_time.split(":")[0])
			if (cHours <= tHours) {
				// console.log('进入')
				cDate = new Date(create_time)
				cDate.setDate(cDate.getDate() - 1)
				cDate = getCommonTime(cDate);
				cYear = cDate.now.year;
				cMonth = cDate.now.month;
				cDay = cDate.now.day;
				cHours = cDate.now.hour;
			}
		}
		shopieReportData = await db.collection('wmzmall-shopie-report').where({
			app_id: app_id,
			shop_id: app_id,
			year: cYear,
			month: cMonth,
			day: cDay
		}).get()
		shopieReportData = shopieReportData.data
		if (shopieReportData.length === 0) {
			console.log('不存在')
			date_time = new Date(cYear + "-" + cMonth + "-" + cDay + ' 00:00:00');
			await db.collection('wmzmall-shopie-report').add({
				app_id: app_id,
				shop_id: app_id,
				year: cYear,
				month: cMonth,
				day: cDay,
				money: money,
				pay_count: pay_count,
				refound_money: refound_money,
				refound_count: refound_count,
				count: 1,
				create_time: new Date().getTime(),
				date_time: date_time.getTime(),
				data: [rechargeDatas[i]],
				ex_month_report: false,
				wechat_count: wechat_count,
				alipay_count: alipay_count,
				wechat_money: wechat_money,
				alipay_money: alipay_money,
				sm_count: sm_count,
				sm_money: sm_money,
				lj_count: lj_count,
				lj_money: lj_money,
				dd_count: dd_count,
				dd_money: dd_money,
				cz_count: cz_count,
				cz_money: cz_money,
				hy_count: hy_count,
				hy_money: hy_money,
				ye_count: ye_count,
				ye_money: ye_money
			})
		} else {
			console.log('已存在')
			shopieReportData = shopieReportData[0]
			console.log(shopieReportData)
			let count = shopieReportData.count + 1
			let data = shopieReportData.data
			data.push(rechargeDatas[i])
			data.sort((a, b) => (a.create_time < b.create_time) ? 1 : -1);
			await db.collection('wmzmall-shopie-report').doc(shopieReportData._id).update({
				money: shopieReportData.money + money,
				count: count,
				data: data,
				refound_money: shopieReportData.refound_money + refound_money,
				pay_count: shopieReportData.pay_count + pay_count,
				refound_count: shopieReportData.refound_count + refound_count,
				ex_month_report: false,
				wechat_count: shopieReportData.wechat_count + wechat_count,
				alipay_count: shopieReportData.alipay_count + alipay_count,
				wechat_money: shopieReportData.wechat_money + wechat_money,
				alipay_money: shopieReportData.alipay_money + alipay_money,
				sm_count: shopieReportData.sm_count + sm_count,
				sm_money: shopieReportData.sm_money + sm_money,
				lj_count: shopieReportData.lj_count + lj_count,
				lj_money: shopieReportData.lj_money + lj_money,
				dd_count: shopieReportData.dd_count + dd_count,
				dd_money: shopieReportData.dd_money + dd_money,
				cz_count: shopieReportData.cz_count + cz_count,
				cz_money: shopieReportData.cz_money + cz_money,
				hy_count: shopieReportData.hy_count + hy_count,
				hy_money: shopieReportData.hy_money + hy_money,
				ye_count: shopieReportData.ye_count||0 + ye_count,
				ye_money: shopieReportData.ye_money||0 + ye_money
			})
		}
		await db.collection('wmzmall-shopie').doc(rechargeDatas[i]._id).update({
			is_report: true
		})
	}



	/* let res=await db.collection('wmzmall-shopie-report').orderBy('create_time','desc').limit(1).get()
	res=res.data
	let pay_count=0
	let refound_count=0
	for(var i in res){
		if(!res[i].pay_count){
			pay_count=0
			refound_count=0
			let data=res[i].data
			let money=res[i].money
			for(var s in data){
				if(data[s].type===0){
					pay_count++
				}else{
					refound_count++
				}
			}
			console.log(pay_count)
			console.log(refound_count)
			await db.collection('wmzmall-shopie-report').doc(res[i]._id).update({
					pay_count:pay_count,
					refound_count:refound_count
				}) 
		}
	} */


	/* let res=await db.collection('wmzmall-shopie-report').orderBy('create_time','desc').limit(1).get()
	res=res.data
	let data=res[0].data
	console.log(data)
	data.sort((a, b) => (a.create_time < b.create_time) ? 1 : -1);
	console.log(data) */
	/* let res=await db.collection('wmzmall-shopie-report').limit(500).get()
	res=res.data
	let total_money=0
	let refound_money=0
	for(var i in res){
		total_money=0
		refound_money=0
		let data=res[i].data
		let money=res[i].money
		for(var s in data){
			if(data[s].type===0){
				total_money=data[s].money*1+total_money*1
			}else{
				refound_money=data[s].money*1+refound_money*1
			}
		}
		if(money!=total_money){
			console.log('不等于')
			console.log(money)
			console.log(total_money)
			console.log(refound_money)
			console.log(res[i]._id)
			await db.collection('wmzmall-shopie-report').doc(res[i]._id).update({
				money:total_money,
				refound_money:refound_money
			})
		} 
	} */


	//设置支付笔数
	/* let res = await db.collection('wmzmall-shopie-report').where({
		wechat_money: dbCmd.exists(false)
	}).limit(500).get()
	res = res.data
	console.log(res.length)
	for (var i in res) {
		let data = res[i].data
		let wechat_count = 0
		let alipay_count = 0
		let wechat_money = 0
		let alipay_money = 0
		let sm_count = 0
		let sm_money = 0
		let lj_count = 0
		let lj_money = 0
		let dd_count = 0
		let dd_money = 0
		let cz_count = 0
		let cz_money = 0
		let hy_count = 0
		let hy_money = 0
		for (var j in data) {
			if (data[j].type === 0) {
				if (data[j].way === '微信支付') {
					wechat_count++
					wechat_money = wechat_money + data[j].money
				} else if (data[j].way === '支付宝支付') {
					alipay_count++
					alipay_money = alipay_money + data[j].money
				}
			}
			if (data[j].cate == '收款码订单') {
				sm_count  ++
				sm_money = sm_money + data[j].money
			} else if (data[j].cate == '远程收单订单') {
				lj_count  ++
				lj_money = lj_money + data[j].money
			} else if (data[j].cate == '商城订单') {
				dd_count  ++
				dd_money = dd_money + data[j].money
			} else if (data[j].cate == '充值') {
				cz_count ++
				cz_money = cz_money + data[j].money
			} else if (data[j].cate == '会员') {
				hy_count  ++
				hy_money = hy_money + data[j].money
			}
		}
		await db.collection('wmzmall-shopie-report').doc(res[i]._id).update({
			wechat_count: wechat_count,
			alipay_count: alipay_count,
			wechat_money: wechat_money,
			alipay_money: alipay_money,
			sm_count: sm_count,
			sm_money: sm_money,
			lj_count: lj_count,
			lj_money: lj_money,
			dd_count: dd_count,
			dd_money: dd_money,
			cz_count: cz_count,
			cz_money: cz_money,
			hy_count: hy_count,
			hy_money: hy_money
		})
	} */
	return event
};